package cn.yunlang.zhangZhongFei.service.impl;

import cn.yunlang.zhangZhongFei.entity.WinAward;
import cn.yunlang.zhangZhongFei.mapper.WinAwardMapper;
import cn.yunlang.zhangZhongFei.service.WinAwardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.yunlang.zhangZhongFei.entity.ExchangeAward;
import cn.yunlang.zhangZhongFei.service.ExchangeAwardService;
import cn.yunlang.zhangZhongFei.util.CheckEmptyUtil;
import cn.yunlang.zhangZhongFei.util.JwtUtil;
import cn.yunlang.zhangZhongFei.vo.request.ExchangeAwardVo;
import cn.yunlang.zhangZhongFei.vo.response.R;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 中奖记录表 服务实现类
 * </p>
 *
 * @author ·
 * @since 2025-05-27 09:54:49
 */
@Service
public class WinAwardServiceImpl extends ServiceImpl<WinAwardMapper, WinAward> implements WinAwardService {
    @Resource
    private ExchangeAwardService exchangeAwardService;

    @Override
    public R<IPage<WinAward>> pageWinAward(Integer pageNo, Integer pageSize, Long userId) {
        if (pageNo == null || pageSize == null) {
            return R.error(400, "分页参数不能为空");
        }
        LambdaQueryWrapper<WinAward> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!CheckEmptyUtil.isEmpty(userId), WinAward::getUserId, userId);
        wrapper.orderByDesc(WinAward::getCreateTime);
        IPage<WinAward> page = this.page(new Page<>(pageNo, pageSize), wrapper);
        return R.success(page);
    }

    @Override
    public R<WinAward> getOne(Long winAwardId) {
        if (winAwardId == null) {
            return R.error(400, "ID不能为空");
        }
        WinAward winAward = this.getById(winAwardId);
        if (winAward == null) {
            return R.error(404, "未找到中奖记录");
        }
        return R.success(winAward);
    }

    @Override
    public R<String> exchange(ExchangeAwardVo exchangeAwardVo) {
        if (exchangeAwardVo == null || exchangeAwardVo.getWinAwardId() == null || exchangeAwardVo.getUserId() == null || CheckEmptyUtil.isEmpty(exchangeAwardVo.getAddress())) {
            return R.error(400, "中奖记录ID、用户ID、收货地址不能为空");
        }
        WinAward winAward = this.getById(exchangeAwardVo.getWinAwardId());
        if (!CheckEmptyUtil.isEmpty(winAward) && (Objects.equals(winAward.getUserId(), exchangeAwardVo.getUserId()) || Objects.equals(winAward.getUserId(), new JwtUtil().getUserId()))) {
            ExchangeAward exchangeAward = new ExchangeAward();
            BeanUtils.copyProperties(winAward, exchangeAward, "id", "createTime");
            exchangeAward.setAddress(exchangeAwardVo.getAddress());
            exchangeAward.setState("0");
            boolean save = exchangeAwardService.save(exchangeAward);
            if (save) {
                return R.success("成功兑奖");
            }
        }
        return R.error(500, "兑奖失败");
    }

    @Override
    public R<String> addWinAward(WinAward winAward) {
        if (winAward == null || winAward.getUserId() == null || winAward.getAwardId() == null || CheckEmptyUtil.isEmpty(winAward.getAwardName())) {
            return R.error(400, "用户ID、奖品ID、奖品名称不能为空");
        }
        boolean save = this.save(winAward);
        return save ? R.success("新增成功") : R.error(500, "新增失败");
    }

    @Override
    public R<String> deleteWinAward(Long winAwardId) {
        if (winAwardId == null) {
            return R.error(400, "ID不能为空");
        }
        boolean remove = this.removeById(winAwardId);
        return remove ? R.success("删除成功") : R.error(500, "删除失败");
    }

    @Override
    public R<String> updateWinAward(WinAward winAward) {
        if (winAward == null || winAward.getId() == null || winAward.getUserId() == null || winAward.getAwardId() == null || CheckEmptyUtil.isEmpty(winAward.getAwardName())) {
            return R.error(400, "ID、用户ID、奖品ID、奖品名称不能为空");
        }
        boolean update = this.updateById(winAward);
        return update ? R.success("修改成功") : R.error(500, "修改失败");
    }
}
